package com.yangyu.bare.utils;

import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ServerUtils {
    private static final Logger log = LoggerFactory.getLogger(ServerUtils.class);
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private ServerUtils() {
    }

    public static Date now() {
        return new Date();
    }

    public static String getHostIp() {
        try {
            Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while(true) {
                NetworkInterface netInterface;
                do {
                    do {
                        do {
                            if (!networkInterfaces.hasMoreElements()) {
                                return "127.0.0.1";
                            }

                            netInterface = (NetworkInterface)networkInterfaces.nextElement();
                        } while(netInterface.isLoopback());
                    } while(netInterface.isVirtual());
                } while(!netInterface.isUp());

                Enumeration inetAddresses = netInterface.getInetAddresses();

                while(inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress)inetAddresses.nextElement();
                    if (inetAddress instanceof Inet4Address) {
                        String ip = inetAddress.getHostAddress();
                        if (!ip.endsWith(".1")) {
                            return ip;
                        }
                    }
                }
            }
        } catch (SocketException var5) {
            log.error("获取IP地址异常", var5);
            return "127.0.0.1";
        }
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException var1) {
            log.error("获取主机名异常", var1);
            return "未知";
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date startDate) {
        long nm = 60000L;
        long nh = nm * 60L;
        long nd = nh * 24L;
        long diff = endDate.getTime() - startDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static String delimiterConversion(String value) {
        return value.replaceAll("\\\\", "/");
    }
}